namespace SharpTalk.Core
{
    using System;
    using Base;
    using System.Collections.Generic;
    using System.Runtime.Serialization;

    [DataContract]
    [Serializable]
    public sealed class ChatRoom 
        : ChatRoomBase, IEquatable<ChatRoom>
    {

        [field:NonSerialized]
        private List<Guid> _users;

        /// <summary>
        /// Initializes a new instance of the <see cref="T:SharpTalk.Core.ChatRoom"/> class.
        /// </summary>
        public ChatRoom()
        {
            _users = new List<Guid>();
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="T:SharpTalk.Core.Base.ChatRoomBase"/> class.
        /// </summary>
        internal ChatRoom(Guid guid) 
            : base(guid)
        {
        }
        
        /// <summary>
        /// Adds a <see cref="T:SharpTalk.Core.User"/> to the <see cref="T:SharpTalk.Core.ChatRoom"/> users list.
        /// </summary>
        /// <param name="user">The <see cref="T:SharpTalk.Core.User"/> to add.</param>
        public bool AddUser(Guid user)
        {
            if (_users == null) _users = new List<Guid>();

            if(!_users.Contains(user))
            {
                _users.Add(user);
                return true;
            }

            return false;
        }
        /// <summary>
        /// Removes a  <see cref="T:SharpTalk.Core.User"/> from the <see cref="T:SharpTalk.Core.ChatRoom"/> users list.
        /// </summary>
        /// <param name="user">The <see cref="T:SharpTalk.Core.User"/> to remove.</param>
        /// <returns>True if success otherwise false</returns>
        public bool RemoveUser(Guid user)
        {
            return _users!=null && _users.Contains(user) && _users.Remove(user);
        }
        /// <summary>
        /// Removes all <see cref="T:SharpTalk.Core.User"/> from the <see cref="T:SharpTalk.Core.ChatRoom"/>.
        /// </summary>
        public void RemoveAllUsers()
        {
            if(_users == null) return;
            _users.Clear();
        }
        /// <summary>
        /// Obtains the <see cref="T:SharpTalk.Core.User"/> count currently in the <see cref="T:SharpTalk.Core.ChatRoom"/>.
        /// </summary>
        [IgnoreDataMember]
        public int UserCount
        {
            get { return _users == null ? 0 : _users.Count; }
        }
        /// <summary>
        /// Obtains the user list or null if none.
        /// </summary>
        [IgnoreDataMember]
        public List<Guid> Users
        {
            get { return _users; }
        }
        /// <summary>
        /// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:SharpTalk.Core.ChatRoom"/>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"/> that represents the current <see cref="T:SharpTalk.Core.ChatRoom"/>.
        /// </returns>
        /// <filterpriority>2</filterpriority>
        public override string ToString()
        {
            return Name;
        }
        /// <summary>
        /// Indicates whether the current object is equal to another object of the same type.
        /// </summary>
        /// <returns>
        /// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
        /// </returns>
        /// <param name="other">An object to compare with this object.</param>
        public bool Equals(ChatRoom other)
        {
            return base.Equals(other);
        }
        /// <summary>
        /// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
        /// </returns>
        /// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>. </param><filterpriority>2</filterpriority>
        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            return Equals(obj as ChatRoom);
        }

        /// <summary>
        /// Serves as a hash function for a particular type. 
        /// </summary>
        /// <returns>
        /// A hash code for the current <see cref="T:System.Object"/>.
        /// </returns>
        /// <filterpriority>2</filterpriority>
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public static bool operator ==(ChatRoom left, ChatRoom right)
        {
            return Equals(left, right);
        }

        public static bool operator !=(ChatRoom left, ChatRoom right)
        {
            return !Equals(left, right);
        }
    }
}